package com.yourbatis.syntax;

import com.yourbatis.interpreter.Environment;

import java.util.ArrayList;
import java.util.List;

public class FunctionCall implements Expression {
    private final String functionName;
    private final List<Expression> arguments;

    public FunctionCall(String functionName, List<Expression> arguments) {
        this.functionName = functionName;
        this.arguments = arguments;
    }

    @Override
    public Object eval() {
        FunctionObject function = Environment.getFun(functionName);
        List<Object> args = new ArrayList<>();
        for (Expression arg : arguments) {
            args.add(arg.eval());
        }
        return function.call(args);
    }
}
